import java.util.ArrayList;
import java.io.*;

import javax.swing.JOptionPane;

/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * The main data holder for <code>EBookOrganizer</code>.
 * </P>
 * @version 0.1
 */
public class Organizer implements java.io.Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8409549974195306682L;
	
	
	private ArrayList<Folder> folders;
	private SettingsModel settings = new SettingsModel();
	
	
	public Organizer() {
		folders = new ArrayList<Folder>();
		
	}
	
	public void addFolder(Folder folder) {
		folders.add(folder);
	}
	
	public Folder getFolder( int index ) {
		return folders.get(index);
	}
	
	public int getNumberOfFolders() {
		return folders.size();
	}
	
	public void clearFolders() {
		folders.clear();
	}
	
	public void removeFolder(Folder folder) {
		folders.remove( folder );
	}
	
	public SettingsModel getSettingsModel() {
		return settings;
	}
	
	public void load( File file ) {
		Organizer org;
		try {
			FileInputStream fileInput = new FileInputStream( file );
			ObjectInputStream objectInput = new ObjectInputStream(fileInput);
			
			org = (Organizer) objectInput.readObject();
			
			objectInput.close();
			
			if (org == null) {
				JOptionPane.showMessageDialog(null, "Error loading");
			}
			else { //setup this Organizer
				this.folders = org.folders;
				this.settings = org.settings;
			}
		}
		catch( IOException e) {
			System.out.println(e);
		}
		catch( ClassNotFoundException e) {
			System.out.println(e);
		}
		
	}
	
	public void save( File file ) {
		try {
			FileOutputStream fileOutput = new FileOutputStream( file);
			ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
			
			objectOutput.writeObject( this );
			
			objectOutput.close();
		}
		catch (IOException e) {
			System.out.println(e);
		}
		
	}
	
	public String toString() {
		String buf = "Organizer: \n";
		for (int i =0; i < folders.size(); i++) {
			buf+="\t"+folders.get(i).display()+"\n";
		}
		return buf;
	}
}
